/*
 * @Author: liwenhao
 * @Date: 2021-05-21 22:45:17
 * @LastEditors: liwenhao
 * @LastEditTime: 2021-05-21 22:55:41
 * @Description:
 */
const {Readable} = require('stream')

// 模拟低层数据
let source = ['lg', 'zce', 'syy']

// 自定义类继承Readable

class MyReadable extends Readable{
  constructor(){
    super()
    this.source = source
  }
  _read() {
    let data = this.source.shift() || null
    this.push(data)
  }
}

// 实例化
let myReadable = new MyReadable(source)

// 暂停模式 使用read读取
// myReadable.on('readable',()=>{
//   let data = null
//   while ((data = myReadable.read(2)) != null) {
//     console.log(data.toString());
//   }
// })

// 流动模式，通过监听data事件
myReadable.on('data',(chunk)=>{
  console.log(chunk.toString());
})